package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.conmmon.R;
import com.qf.entity.User;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @propram: IntelliJ IDEA
 * @description:  用户控制器
 * @author: 秦先发
 * @create: 2021-09-24 14:03
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user) {
        System.out.println(user);
        return R.getR(userService.save(user));
    }

    /**
     * 删除用户
     *
     * @param
     * @return
     */
    @PostMapping("/deleteUser")
    public R deleteUserById(@RequestBody User user) {
        System.out.println(user.getId());
        return R.getR(userService.removeById(user.getId()));
    }

    /**
     * 更新用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody User user) {


        return R.getR(userService.updateById(user));
    }

    /**
     * 分页查询用户信息
     *
     * @param page
     * @return
     */
    @PostMapping("/selectUserPage")
    public R selectUser(@RequestBody Page<User> page) {
        Page<User> list = userService.page(page);
        return R.getR(list);
    }
        @PostMapping("/selectAllUser")
    public R selectAllUser(){
        return R.saveR("data",userService.list());
    }
    /**
     * 根据id查询用户信息
     */
    @GetMapping ("/selectUserById")
    public R selectUserById(@RequestParam Integer  id) {
        System.out.println(id);
        User byId = userService.getById(id);
        if (byId == null) {
            return R.error();
        } else {
            R ok = R.ok();
            ok.put("user", byId);
            return ok;
        }
    }
    /**
     * 批量删除
     */
    @PostMapping("/deleteUsers")
    public R deleteUsers(@RequestBody List<String> ids){
        int i = userService.getBaseMapper().deleteBatchIds(ids);
        R r = R.getR(i);
        return r;
    }

}
